import axios from 'axios';

import * as Config from './Config.js';


import Vue from 'vue';

const local = localStorage;


Vue.prototype.handelUpdateRole = function (roleid) {
	console.log(local.permissionIds)
	console.log(999)
    for (let n of local.permissionIds.split(',')) {
        if (parseInt(n) === roleid) {
            return true
        }
    }
}



let base = Config.g_base_url;


export const requestBody = (url, ...body) => {

    var querystring = require('querystring');

    return axios.post(`${url}`, ...body)
        .then(res => {

            if (res.status != 200) {
                return {
                    retCode: false,
                    message: "网络数据异常,错误代码是" + res.status,
                    data: {}
                }

            }

            if (res.data.status != 200) {
                return {
                    retCode: false,
                    message: res.data.message,
                    data: {}
                }

            }

            return {
                retCode: true,
                message: '',
                data: res.data.data
            }





        })
        .catch(error => {
            
                        return {
                            retCode: false,
                            message: `网络请求异常 请求路径 ${urlPath},错误信息 ${error}`,
                            data: {}
                        }
            
            
                    });
            
};
export const  uploadImg = (baseUrl,urlPath, params) =>{
	var querystring = require('querystring');
    console.log(JSON.stringify(params))
 	console.log(params)
 	return new Promise((reject, rej) => {
		axios({
			url: baseUrl + urlPath,
			method: 'POST',
			data: params,
		  	headers: {
			    'Content-Type': 'multipart/form-data'
		  	}
		}).then(res => {
			console.log(res)
			if (res.status != 200) {
                return {
                    retCode: false,
                    message: "网络数据异常,错误代码是" + res.status,
                    data: {}
                }

            }
			console.log(res.data)
			console.log(5555555555)
            if (res.data.status != 200) {
            	console.log(res.data)
            	rej({
            		retCode: false,
                    message: res.data.message,
                    data: {}
            	})
                

            }
			console.log(res.data)
			reject({
				retCode: true,
                message: '',
                data: res.data
			})
            
			
		})
	})
    

}
export const requestForm = (baseUrl,urlPath, params) => {
    var querystring = require('querystring');
    console.log(JSON.stringify(params))
 	console.log(typeof params)
    return axios.post(`${baseUrl}${urlPath}`,
            querystring.stringify(params), {
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
                }
            })
        .then(res => {

            if (res.status != 200) {
                return {
                    retCode: false,
                    message: "网络数据异常,错误代码是" + res.status,
                    data: {}
                }

            }

            if (res.data.status != 200) {
                return {
                    retCode: false,
                    message: res.data.message,
                    data: {}
                }

            }

            return {
                retCode: true,
                message: '',
                data: res.data.data
            }

        })
        .catch(error => {

            return {
                retCode: false,
                message: `网络请求异常 请求路径 ${urlPath},错误信息 ${error}`,
                data: {}
            }


        });

};
export const requestGet = (baseUrl,urlPath,params) => {

    
       return axios.get(`${baseUrl}${urlPath}`,
            {
        　　  params: params
            }
              )
           .then(res => {
   
               if (res.status != 200) {
                   return {
                       retCode: false,
                       message: "网络数据异常,错误代码是" + res.status,
                       data: {}
                   }
   
               }
   
               if (res.data.status != 200) {
                   return {
                       retCode: false,
                       message: res.data.message,
                       data: {}
                   }
   
               }
   
               return {
                   retCode: true,
                   message: '',
                   data: res.data.data
               }
   
           })
           .catch(error => {
   
               return {
                   retCode: false,
                   message: `网络请求异常 请求路径 ${urlPath},错误信息 ${error}`,
                   data: {}
               }
   
   
           });
   
};


    
